<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button size="small" type="success"  icon="el-icon-share" @click="uploadKingdee(this.selection)">批量上传金蝶</el-button>
        <el-button size="small" v-loading="stockLoading" @click="reloadStock? getStockReloadResult(true): doStockSync(searchForm)" :icon="reloadStock? 'el-icon-loading': 'el-icon-refresh'">{{ reloadStock? '同步中，点击刷新': '一键上传收款单' }}</el-button>
      </template>

      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
        <!--        <el-button size="mini" type="text" style="color:var(&#45;&#45;el-color-success)" icon="el-icon-share" @click="uploadKingdee(scope.row.billNo)">上传金蝶</el-button>-->
      </template>
      <template #table-isUpload="scope">
        <el-tag :type="scope.row.isUpload === 'yes'? 'success': 'info'" >{{ scope.row.isUpload === 'yes'? '已上传': '未上传' }}</el-tag>
      </template>

    </vb-page>
  </div>
  <upload-dialog ref="upload_dialog" upload-method="upload_prdcol_bill" @success="uploadFinish"></upload-dialog>
  <selectRowStockIn ref="selectRowStockIn" @success=""></selectRowStockIn>
</template>

<script>
import uploadDialog from '../components/uploadDialog.vue'
import selectRowStockIn from '../components/selectRowStockInDialog.vue'

export default {
  data() {
    return {
      loading:false,
      selection: null,
      selectionLength:0,
      stockLoading: false,
      reloadStock: false,
    }
  },
  components: {
    uploadDialog,
    selectRowStockIn
  },
  methods: {
    async getStockReloadResult(showMsg) {
      this.stockLoading = true
      try {
        let res = await this.callApi("get_stock_reload_result")
        if (!res.res) return
        this.reloadStock = res.reloadStock
        this.reloadStockRes = res.reloadStockRes
        this.reloadStockMsg = res.reloadStockMsg
        this.reloadStockStartTime = res.reloadStockStartTime
        this.reloadStockEndTime = res.reloadStockEndTime
        if (showMsg) {
          if (this.reloadStock) {
            this.$message.info('数据正在上传中')
          } else {
            if (this.reloadStockRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadStockMsg)
            }
          }
        }
      } finally {
        this.stockLoading = false
      }
    },
    /**
     * 同步销售单
     * */
    async doStockSync(searchForm) {
      try {
        let msg = '';
        if (this.reloadStockStartTime) {
          msg += `<span style="font-weight: bold;">一键上传，耗时较长，是否继续执行？</span><br />
          前次同步${ this.reloadStockRes? '成功!': `<span style="color: var(--danger)">失败!</span>` } ${ this.reloadStockEndTime }
          ${ this.reloadStockRes? '': `<br />${ this.reloadStockMsg }` }
          <br />`;
        } else {
          msg = '一键上传，耗时较长，是否继续执行？';
        }
        await this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          dangerouslyUseHTMLString: true
        })
      } catch(e) {
        return
      }
      try {
        this.loading = true
        let res = await this.callApi('upload_kingdee_collection_by_one_click',searchForm)
        if (!res.res) return
        this.reloadStock = true
        this.$message.success('已经开始同步')
      } finally {
        this.loading = false
      }
    },
    async uploadKingdee(billNoList){
      if (!this.selection || this.selection === 0){
        this.$message.error('没有选择行')
        return
      }
      const arrBillNo = [];
      for (let i = 0;i<billNoList.length;i++){
        arrBillNo.push(billNoList[i].billNum)
      }
      try {
        this.$message.warning('正在上传')
        let res = await this.callApi('upload_kingdee_collection',{billNoList:arrBillNo})
        console.log(res)
        if(!res.res){
          return
        }
        this.$message.success('上传成功')
        this.$refs.page.reload()
      }finally {
        this.loading = false
      }
    },
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length;
      this.selectCount=0
      for (var i=0;i<this.selectionLength;i++){
        this.selectCount+=data[i].count
      }
      this.selectCount = parseFloat(this.selectCount).toFixed(2)
      // console.log(this.selection)
      // console.log(data[0].billNo)
    }
  }
}
</script>

<style scoped>

</style>
